package com.erp.erp_mask.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.erp.erp_mask.mapper.TCategoryMapper;
import com.erp.erp_mask.pojo.TCategory;
import com.erp.erp_mask.service.TCategoryService;
import com.erp.erp_mask.vo.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * @BelongsProject: erp
 * @BelongsPackage: com.erp.erp_mask.controller
 * @Author: wang
 * @CreateTime: 2024-05-24  14:28
 * @Description: TODO
 * @Version: 1.0
 * @effect:
 */
@RestController
@RequestMapping("/cate")
public class TCategoryController {
    @Resource
    private TCategoryService tCategoryService;
    @Resource
    private TCategoryMapper tCategoryMapper;
    @GetMapping("/index")
    public ModelAndView index(){
     return new ModelAndView("/duct/proclassify");
    }
    @GetMapping("/serach")
    public Result search(){
        return Result.success(tCategoryService.searchlist());
    }
    @GetMapping("/serachlist")
    public Result searchlist(){
        LambdaQueryWrapper<TCategory> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TCategory::getParentLevel,1);
        return Result.success(tCategoryService.list(wrapper));
    }
    @PostMapping("/addlist")
    public Result addlist(@RequestBody TCategory tCategory){
        try{
            tCategoryMapper.insert(tCategory);
            return Result.success("添加成功!");
        }catch (Exception e){
            return Result.fail("添加失败!");
        }
    }
    @GetMapping("/select")
    public Result selectlist(){
        return Result.success(tCategoryService.list());
    }
    @PutMapping("/edit")
    public Result edit(@RequestBody TCategory tCategory){
        try{
            tCategoryService.updateById(tCategory);
            return Result.success("修改成功!");
        }catch (Exception e){
            return Result.fail("修改失败!");
        }
    }
    @DeleteMapping({"/del"})
    public Result del(Integer id){
        try {
            tCategoryService.removeById(id);
            return Result.success("删除成功!");
        }catch (Exception e){
            return Result.fail("删除失败!");
        }


    }
}
